package com.stsdai.manager.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stsdai.common.pojo.StsdaiResult;
import com.stsdai.manager.mapper.ContentMapper2;
import com.stsdai.manager.pojo.Content;
import com.stsdai.manager.redis.RedisUtils;
import com.stsdai.manager.service.ContentService;


@Service
public class ContentServiceImpl extends BaseServiceImpl<Content> implements ContentService {

	private static final ObjectMapper MAPPER = new ObjectMapper();
	
	@Resource
	private ContentMapper2 contentMapper2;

	@Override
	public StsdaiResult<Content> queryContentByPage(Integer page, Integer rows, Long categoryId) {
		// 设置分页参数
		PageHelper.startPage(page, rows);

		// 设置查询条件
		Content content = new Content();
		content.setCategoryId(categoryId);

		// 执行查询
		List<Content> list = super.queryListByWhere(content);

		// 封装返回对象taoResult
		StsdaiResult<Content> stsdaiResult = new StsdaiResult<>();
		// 设置结果集
		stsdaiResult.setRows(list);

		// 设置总记录数
		PageInfo<Content> pageInfo = new PageInfo<>(list);
		stsdaiResult.setTotal(pageInfo.getTotal());

		return stsdaiResult;
	}

	@Autowired
	private RedisUtils redisUtils;

	@Value("${STSDAI_AD_KEY}")
	private String STSDAI_AD_KEY;

	@Override
	public String queryContentByCategoryId(Long categoryId) {
		try {
			// 1.从redis中命中
			String redisJson = this.redisUtils.get(this.STSDAI_AD_KEY);
			// 判断是否查询到
			if (StringUtils.isNotBlank(redisJson)) {
				// 如果查到，直接返回数据
				return redisJson;
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// 2.执行原有的逻辑，从MySQL中查询
		// 设置查询条件
		Content param = new Content();
		param.setCategoryId(categoryId);
		// 执行查询
		List<Content> list = super.queryListByWhere(param);

		// 声明存放大广告数据的List《Map》容器
		List<Map<String, Object>> results = new ArrayList<>();

		// 遍历结果集，把查询的内容数据封装到list《Map》中
		for (Content content : list) {
			// 声明Map
			Map<String, Object> map = new HashMap<>();

			// 给Map设置数据
			map.put("srcB", content.getPic());
			map.put("height", 400);
			map.put("alt", "");
			map.put("width", 670);
			map.put("src", content.getPic());
			map.put("widthB", 550);
			map.put("href", content.getUrl());
			map.put("heightB", 400);

			// 把Map放到results容器中
			results.add(map);

		}

		// 把results转为json格式的数据
		String json = "";
		try {
			json = MAPPER.writeValueAsString(results);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			// 3.把查询到的结果放到redis中
			this.redisUtils.set(this.STSDAI_AD_KEY, json, 60 * 60 * 24);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 返回大广告数据
		return json;
	}

	@Override
	public Content queryLeftYouJi() {
		
		Content content = contentMapper2.queryLeftYouJi();
		return content;
	}

	@Override
	public Content queryLeftShiLing() {
		
		Content content = contentMapper2.queryLeftShiLing();
		return content;
	}

	@Override
	public List<Content> queryLeftJingPing() {
		List<Content> list = contentMapper2.queryLeftJingPing();
		return list;
	}

	@Override
	public List<Content> queryLeftItemXiangQing() {
		List<Content> list = contentMapper2.queryLeftItemXiangQing();
		return list;
	}
	
	

}
